<?php
/**
 * @copyright (C)2016-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date 2017年12月26日
 *  文章类型控制器
 */
namespace app\admin\controller\content;

use core\basic\Controller;
use app\admin\model\content\ContentTypeModel;

class ContentTypeController extends Controller
{

    private $count;

    private $blank;

    private $outData = array();

    private $model;

    public function __construct()
    {
        $this->model = new ContentTypeModel();
    }

    // 文章类型列表
    public function index()
    {
        $this->assign('list', true);
        $tree = $this->model->getList();
        $types = $this->makeTypeList($tree);
        $this->assign('types', $types);

        
        // 文章类型下拉表
        $type_tree = $this->model->getSelect();
        $type_select = $this->makeTypeSelect($type_tree);
        $this->assign('type_select', $type_select);
        $this->display('content/contenttype.html');
    }

    public function tree(){
        header('content-type: json; charset=utf-8');
        $tree = $this->model->getSelect('children');
        return json(0,$tree);
    }

    // 生成无限级文章类型列表
    private function makeTypeList($tree)
    {
        // 循环生成
        foreach ($tree as $value) {
            $this->count ++;
            $this->outData[$this->count] = new \stdClass();
            foreach ($value as $k => $v) {
                $this->outData[$this->count]->$k = $v;
            }
            
            if ($value->son) {
                $this->outData[$this->count]->son = true;
            } else {
                $this->outData[$this->count]->son = false;
            }
            
            // 子菜单处理
            if ($value->son) {
                $this->blank .= '　　';
                $this->makeTypeList($value->son);
            }
        }
        
        // 循环完后回归缩进位置
        $this->blank = substr($this->blank, 6);
        return $this->outData;
    }

    // 文章类型增加
    public function add()
    {
        if ($_POST) {
            // 获取数据
            $pid = post('pid', 'int');
            $name = post('name');

            if( $pid == 0 ){
                $level = 1;
            }else{
                $level = $this->model->getType($pid)->level + 1;
            }

            $status = post('status', 'int');
            $sorting = post('sorting', 'int');

            $pic = post('pic');
            $subtitle = post('subtitle');
            $tag = post('tag');


            if (! $pid) { // 父编码默认为0
                $pid = 0;
            }

            if (! $name) {
                alert_back('类型名称不能为空！');
            }


            // 构建数据
            $data = array(
                'acode' => session('acode'),
                'pid' => $pid,
                'level' => $level,
                'name' => $name,
                'status' => $status,
                'sorting' => $sorting,

                'subtitle' => $subtitle,
                'tag' => $tag,

                'pic' => $pic,

                'create_user' => session('username'),
                'update_user' => session('username')
            );
        }

        // 执行添加
        if ($this->model->addType($data)) {

            $this->log('新增数据文章类型成功！');
            success('新增成功！', url('/admin/ContentType/index'));
        } else {
            $this->log('新增数据文章类型失败！');
            error('新增失败！', - 1);
        }

    }

    // 生成文章类型下拉选择
    private function makeTypeSelect($tree, $selectid = null)
    {
        $list_html = '';
        foreach ($tree as $value) {
            // 默认选择项
            if ($selectid == $value->id) {
                $select = "selected='selected'";
            } else {
                $select = '';
            }
            if (get('id') != $value->id) { // 不显示本身，避免出现自身为自己的父节点
                $list_html .= "<option value='{$value->id}' $select>{$this->blank}{$value->name}</option>";
            }
            // 子菜单处理
            if ($value->son) {
                $this->blank .= '　　';
                $list_html .= $this->makeTypeSelect($value->son, $selectid);
            }
        }
        // 循环完后回归位置
        $this->blank = substr($this->blank, 0, - 6);
        return $list_html;
    }

    // 文章类型删除
    public function del()
    {
        // 执行批量删除
        if ($_POST) {
            if (! ! $list = post('list')) {
                if ($this->model->delTypeList($list)) {
                    $this->log('批量删除栏目成功！');
                    success('批量删除成功！', - 1);
                } else {
                    $this->log('批量删除栏目失败！');
                    error('批量删除失败！', - 1);
                }
            } else {
                alert_back('请选择要删除的栏目！');
            }
        }
        
        if (! $id = get('id', 'var')) {
            error('传递的参数值错误！', - 1);
        }
        if ($this->model->delType($id)) {
            $this->log('删除数据文章类型' . $id . '成功！');
            success('删除成功！', - 1);
        } else {
            $this->log('删除数据文章类型' . $id . '失败！');
            error('删除失败！', - 1);
        }
    }

    // 文章类型修改
    public function mod()
    {
        if (! ! $submit = post('submit')) {
            switch ($submit) {
                case 'sorting': // 修改列表排序
                    $listall = post('listall');
                    if ($listall) {
                        $sorting = post('sorting');
                        foreach ($listall as $key => $value) {
                            if ($sorting[$key] === '' || ! is_numeric($sorting[$key]))
                                $sorting[$key] = 255;
                            $this->model->modTypeSorting($value, "sorting=" . $sorting[$key]);
                        }
                        $this->log('批量修改栏目排序成功！');
                        success('修改成功！', - 1);
                    } else {
                        alert_back('排序失败，无任何内容！');
                    }
                    break;
            }
        }
        
        if ( (! $id = get('id', 'var')) && !get('pid','int') ) {
            error('传递的参数值错误！', - 1);
        }
        
        // 单独修改状态
        if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
            if ($this->model->modType($id, "$field='$value',update_user='" . session('username') . "'")) {
                $this->log('修改数据文章类型' . $id . '状态' . $value . '成功！');
                location(- 1);
            } else {
                $this->log('修改数据文章类型' . $id . '状态' . $value . '失败！');
                alert_back('修改失败！');
            }
        }
        
        // 修改操作
        if ($_POST) {
            
            // 获取数据
            $name = post('name');

            $status = post('status', 'int');
            $sorting = post('sorting', 'int');

            $pic = post('pic');
            $subtitle = post('subtitle');
            $tag = post('tag');

            if (! $name) {
                alert_back('类型名称不能为空！');
            }

            
            // 构建数据
            $data = array(
                'name' => $name,
                'status' => $status,
                'sorting' => $sorting,

                'subtitle' => $subtitle,
                'tag' => $tag,
                'pic' => $pic,

                'update_user' => session('username')
            );
            
            // 执行添加
            if ($this->model->modType($id, $data)) {

                
                $this->log('修改数据文章类型' . $id . '成功！');
                success('修改成功！', url('/admin/ContentType/index'));
            } else {
                location(- 1);
            }
        } else { // 调取修改内容
            $this->assign('mod', true);


            if( get('pid','int') ){
                $pid = get('pid','int');
                $pType = $this->model->getType($pid);
                if (! $pType) error('编辑的内容已经不存在！', - 1);
                $type = null;
                $type->pid = $pType->id;
                $type->sorting = 255;
                $type->status = 1;
                $this->assign('type', $type);

                // 父编码下拉选择
                $type_tree = $this->model->getSelect();
                $type_select = $this->makeTypeSelect($type_tree, $pType->id);
                $this->assign('type_select', $type_select);
                $this->assign('method', 'add');

                return $this->display('content/contenttype.html');
            }

            $type = $this->model->getType($id);
            if (! $type) error('编辑的内容已经不存在！', - 1);

            $this->assign('type', $type);
            
            // 父编码下拉选择
            $type_tree = $this->model->getSelect();
            $type_select = $this->makeTypeSelect($type_tree, $type->pid);
            $this->assign('type_select', $type_select);
            $this->assign('method', 'mod');

            $this->display('content/contenttype.html');
        }
    }

}
